package com.whq.knowverywell.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 跟日期相关的工具类
 * @author wanghuaiqiang
 *
 */
public class DateUtil {
    
    public static final String FEED_DATE_FORMAT = "MM-dd HH:mm a";
    
	/**
	 * 按格式把String转化成Date对象
	 * @param aMask 时间格式
	 * @param strDate
	 * @return
	 */
	public static final Date convertStringToDate(String aMask, String strDate){
		SimpleDateFormat df = null;
		Date date = null;
		if (strDate == null || "".equals(strDate)) {
			return date;
		}
		df = new SimpleDateFormat(aMask);
		try {
			date = df.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 按格式把Date转化成String对象
	 * @param aMask 时间格式
	 * @param aDate
	 * @return
	 */
	public static final String convertDateToString(String aMask, Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (aDate != null) {
			df = new SimpleDateFormat(aMask);
			returnValue = df.format(aDate);
		}
		return returnValue;
	}
	
	/**
	 * 按格式把Date转化成Integer对象
	 * @param aMask 时间格式
	 * @param aDate
	 * @return
	 */
	public static final Integer convertDateToInteger(String aMask, Date aDate) {
		String strDate = convertDateToString(aMask, aDate);
		Integer iDate = null;
		if (strDate != null && !"".equals(strDate)) {
			iDate = Integer.valueOf(strDate);
		}
		return iDate;
	}
	
	/**
     * 获得传入日期 date 偏移 offsetDay 天数的00:00:00
     * @param date
     * @param offsetDay 偏移天数
     * @return
     */
    public static Date getOffsetDateBegin(Date date, int offsetDay) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.add(Calendar.DATE, offsetDay);
        return c.getTime();
    }
	
    /**
     * 获取某天的开始时间戳
     * @param format
     * @param dateStr
     * @return
     */
	public static long getDateBeginTimestamp(String format, String dateStr) {
		Date dt = convertStringToDate(format, dateStr);
		if (dt == null) {
			return 0l;
		} else {
			Date realDt = getOffsetDateBegin(dt, 0);
			return realDt.getTime();
		}
		
	}
	
	/**
	 * 获取某天的结束时间戳
	 * @param format
     * @param dateStr
	 * @return
	 */
	public static long getDateEndTimestamp(String format, String dateStr)  {
		Date dt = convertStringToDate(format, dateStr);
		if (dt == null) {
			return 0l;
		} else {
			Date realDt = getOffsetDateBegin(dt, 1);
			return realDt.getTime();
		}
	}

	/**
	 * 传入时间距离当前时间的差距描述
	 * 
	 * @param cmpDate
	 * @return
	 */
	public static String dataTime2NowDuration(Date cmpDate) {
		if (cmpDate == null) {
			return "未知";
		}
		
		Date now = new Date();
		long duration = now.getTime() - cmpDate.getTime();
		if (duration < (60 * 1000)) {
			return "刚刚";
		} else if (duration >= 60 * 1000 && duration < 3600 * 1000) {
			return duration/(60 * 1000) + "分钟前";
		} else if (duration >= 3600 * 1000 && duration < 3600 * 24 * 1000) {
			return duration/(3600 * 1000) + "小时前";
		} else if (duration >= 3600 * 24 * 1000 && duration < 3600 * 24 * 30 * 1000) {
			return duration/(3600 * 24 * 1000) + "天前";
		}else if (duration >= 3600 * 24 * 30 * 1000 && duration < 3600 * 24 * 30 * 12 * 1000) {
			return duration/(3600 * 24 * 30 * 1000) + "月前";
		}else {
			return "很久以前";
		}
	}
	
	/**
	 * 判断是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean chkSameDay(Date date1, Date date2) {
		if ( (date1 == null) || (date2 == null) ) {
			return false;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date1).equals(sdf.format(date2));
	}
	
	/**
	 * 得到date所在月份的第一天00:00:00
	 * @param date
	 * @return 
	 */
	public static Date getMonthFirstDateOfOneDate(Date date) {
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.set(Calendar.DATE, 1);
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到date所在月份的最后一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getMonthLastDateOfOneDate(Date date) {
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.set(Calendar.DATE, 1);// 设为当前月的1号
		c.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		c.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到 date 所在月份下月的第一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getNextMonthFirstDateOfOneDate(Date date) {
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.MONTH, 1);	// 加一个月
		c.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
	
	/**
	 * 得到 date 所在月份上月的第一天00:00:00
	 * @param date
	 * @return
	 */
	public static Date getBeforeMonthFirstDateOfOneDate(Date date) {
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.MONTH, -1);	// 加一个月
		c.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
	    
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    c.set(Calendar.MILLISECOND, 0);
	    
	    return c.getTime();
	}
}
